package wmh.day0109.utils;

import  wmh.day0109.entity.User;

import java.sql.*;


public class BaseDao {
    protected static String driver;
    protected static String url;
    protected static String username;
    protected static String password;

    protected Connection conn;

    protected PreparedStatement pstmt;

    protected ResultSet rs;
    // 1、建立数据库连接
    public Connection getConn(){
        // 获取driver   url     username    password 这四个参数对应值
        driver = ConfigManager.getInstance().getProperty("orcl.driver");
        url = ConfigManager.getInstance().getProperty("orcl.url");
        username = ConfigManager.getInstance().getProperty("orcl.username");
        password = ConfigManager.getInstance().getProperty("orcl.password");
        try {
            // ①加载数据库驱动
            Class.forName(driver);
            // ②建立数据库连接对象
            conn = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    public int saveUser(User user){
        String sql = "insert into lottery(user_name,mark_no,status,phone,open_date) values(?,?,?,?,to_date(?,'yyyy-mm-dd'))";
        return executeUpdate(sql,new Object[]{user.getUsername(),user.getMarkNo(),user.getStatus(),user.getPhone(),user.getDate()});

    }

    public void closeAllConn(Connection conn ,PreparedStatement pstmt ,ResultSet rs ){
        try {
            if (pstmt!=null){
                pstmt.close();
            }
            if (conn!=null){
                conn.close();
            }
            if (rs != null) {
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public  int executeUpdate(String sql,Object...params){
        int result = 0;
        conn = getConn();
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i <params.length ; i++) {
                pstmt.setObject(i+1,params[i]);
            }
            result = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAllConn(conn,pstmt,rs);
        }
        return  result;
    }
    public int queryUserByName(String username){
        User user = null;
        //2.定义查询语句
        String sql = "select id,mark_no, status , phone ,open_date from dept where user_name = ?";
        try {
          //  rs = executeQuery(sql,new Object[]{username});
            //6.遍历rs结果集
            if (rs.next()){
                user = new User();
                //使用set赋值
                user.setId(rs.getInt("id"));
                user.setMarkNo(rs.getString("mark_no"));
                user.setStatus(rs.getInt("status"));
                user.setPhone(rs.getString("phone"));
                user.setDate(rs.getString("open_date"));
                user.setUsername(rs.getString("user_name"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAllConn(conn,pstmt,rs);
        }
        return 0;
    }

    protected ResultSet executeQuery(String sql,Object...params) {
        try {
            conn = getConn();
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            rs = pstmt.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }


}

